package com.meepo.framework.design.list.comments.reply;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.meepo.framework.R;
import com.meepo.framework.design.list.comments.Comment;
import com.meepo.framework.design.list.comments.CommentsView;
import com.meepo.framework.design.xrecyclerview.XRecyclerView;

import java.util.Collections;
import java.util.List;

public class ReplyDialog extends DialogFragment {

    private View mView;

    private final Comment mComment;

    public ReplyDialog(Comment comment) {
        this.mComment = comment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
//        Dialog dialog = super.onCreateDialog(savedInstanceState);
        final Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);


        mView = View.inflate(getActivity(), R.layout.comments_reply_layout, null);

//        View view = LayoutInflater.from(getActivity()).inflate(R.layout.comments_reply_layout, null, false);
//        dialog.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.setContentView(mView);
        dialog.setCanceledOnTouchOutside(true);

        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;

        // todo 动画无效
        lp.windowAnimations = R.style.Animation_BottomIn_BottomOut;

        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.BOTTOM);


        ImageView closeView = mView.findViewById(R.id.close_iv);
        closeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        // 主评论
        CommentsView main = mView.findViewById(R.id.main_comments);

        main.setItemLayoutId(R.layout.comments_reply_main_comment_layout);
        main.addComments(Collections.singletonList(mComment));

        return dialog;
    }

    public void addComments(List<Comment> comments) {
        CommentsView reply = mView.findViewById(R.id.reply_comments);
        reply.addComments(comments);
    }

    public void setNoMore(boolean noMore ) {
        CommentsView reply = mView.findViewById(R.id.reply_comments);
        reply.setNoMore(noMore);
    }

    public void setLoadMoreListener(XRecyclerView.LoadingListener listener) {
        CommentsView reply = mView.findViewById(R.id.reply_comments);
        reply.setLoadingListener(listener);
    }
}
